package com.im.service.meteDeclare.smo;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.im.service.dto.BaseException;
import com.im.service.dto.PageQuery;
import com.im.service.dto.MeteDeclare;
import com.im.service.projectInfo.smo.ProjectInfoQuerySMOImpl;
import com.im.service.meteDeclare.dao.IMeteDeclareQueryDAO;
import com.im.util.JsonUtil;
import com.im.util.ReturnsUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 采购计划查询 SMO
 * @author wangxh
 * 018年3月27日
 */
@Service
public class MeteDeclareQuerySMOImpl implements IMeteDeclareQuerySMO {

  private static final Logger logger = LogManager.getLogger(ProjectInfoQuerySMOImpl.class);

  @Autowired
  private IMeteDeclareQueryDAO meteDeclareQueryDAO;


  @Override
  public PageQuery<MeteDeclare> queryMeteDeclareList(String inJson) throws Exception  {

    try {
      if (!JsonUtil.jsonValidate(inJson)) {
        throw new BaseException(
                new StringBuffer().append("字符串： ").append(inJson).append("不符合JSON格式").toString());
      }
      PageQuery<MeteDeclare> page = JsonUtil.jsonToBean(inJson, PageQuery.class);
      MeteDeclare meteDeclare = JsonUtil.jsonToBean(inJson, MeteDeclare.class);

      Map<String,Object> param = new HashMap<String,Object>();
      param.put("meteDeclare", meteDeclare);
      param.put("page", page);
      PageHelper.startPage(page.getPageNumber(), page.getPageSize());
      List<MeteDeclare> list=meteDeclareQueryDAO.queryMeteDeclareList(param);
      page.setRows(list);
      page.setTotalPages(((Page<MeteDeclare>)list).getPages());
      page.setTotal(((Page<MeteDeclare>)list).getTotal());
      return page;
    } catch (Exception e) {
      if (logger.isErrorEnabled()) {
        logger.error(ReturnsUtil.getStackTrackMessage(e));
      }
      throw new Exception(ReturnsUtil.getStackTrackMessage(e));
    }
  }

  @Override
  public MeteDeclare queryMeteDeclareDetail(MeteDeclare req) throws Exception{

    try {
      Map<String,Object> param = new HashMap<String,Object>();
      param.put("meteDeclare", req);
      MeteDeclare meteDeclareDetail=meteDeclareQueryDAO.queryMeteDeclareDetail(param);
      return meteDeclareDetail;
    } catch (Exception e) {
      if (logger.isErrorEnabled()) {
        logger.error(ReturnsUtil.getStackTrackMessage(e));
      }
      throw new Exception(ReturnsUtil.getStackTrackMessage(e));
    }
  }
}
